from flask import Blueprint
from flask import render_template, request, redirect, session, url_for
from sqlalchemy import or_

from exit import db
from user.models import User
from blog.models import Article, Comment

blog_bp = Blueprint('blog', __name__, url_prefix='/blog')

#首页
@blog_bp.route('/index')
def index():
    #查询所有的博客
    # articles = Article.query.all()
    # return render_template('index.html', articles=articles)

    #分页查找--获取页码对象
    page =int(request.args.get('page', 1))
    paginate = Article.query.paginate(page=page, per_page=10)
    return render_template('index.html', paginate=paginate)

#发表博客
@blog_bp.route('/publish', methods=['GET', 'POST'])
def publish():
    if request.method == 'POST':
        print(request.form)
        title = request.form.get('title')
        content = request.form.get('content')
        user = User.query.filter(User.uname == session.get('username')).first()

        #创建博客对象
        article = Article(title, content, user.uid)
        db.session.add(article)
        db.session.commit()
        return render_template('publish.html')
    else:
        # 判断是否登录
        if session.get('username'):
            return render_template('publish.html')
        else:
            return redirect(url_for('user.login'))

#详情页
@blog_bp.route('/detail')
def detail():
    aid = int(request.args.get('articleid'))
    article = Article.query.get(aid)
    article.looknum += 1
    db.session.commit()
    return render_template('detail.html', article=article)

#点赞
@blog_bp.route('/love')
def love():
    # print(request.args)
    aid = request.args.get('aid')
    article = Article.query.get(aid)
    article.likenum += 1
    db.session.commit()
    return {'code': 2000, 'likenum': article.likenum}

#发表评论
@blog_bp.route('/pubcomment', endpoint='comment', methods=['GET', 'POST'])
def pubcomment():
    if session.get('username'):
        if request.method == 'POST':
            print(request.form)
            content = request.form.get('content')
            aid = int(request.form.get('aid'))
            user = User.query.filter(User.uname == session.get('username')).first()
            comment = Comment(content, user.uid, aid)

            # 这两句的作用是每当你发表一次评论浏览量就+1，所以在这里先-1，这样发表评论的时候浏览量就不会发生变化了
            article = Article.query.get(aid)
            article.looknum -= 1

            db.session.add(comment)
            db.session.commit()
            # 重定向到详情页，将这里的aid传递到详情页的articleID
            return redirect(url_for('blog.detail') + '?articleid=' + str(aid))
    else:
        return redirect(url_for('user.blog'))

#评论区的点赞
@blog_bp.route('/comprize', endpoint='comprize')
def commprize():
    print(request.args)
    cid = request.args.get('cid')
    comment = Comment.query.get(cid)
    comment.likenum += 1
    db.session.commit()
    return {'code': 2000, 'likenum': comment.likenum, 'cid': comment.cid}

#查找
@blog_bp.route('/search')
def search():
    # print(request.args)
    keyword = request.args.get('keyword')
    #分页
    page = int(request.args.get('page',1))
    paginate = Article.query.filter(or_(Article.title.like(f'%{keyword}%'),Article.content.like(f'%{keyword}%'))).paginate(page=page, per_page=6)
    return render_template('search.html', paginate=paginate, keyword=keyword)